
import java.io.Serializable;
import java.util.Random;

public class CeasarShift extends Message implements Serializable {

    private int ceasarKey;

    public CeasarShift(User u, int friend, String mText) {
        super(u, friend, mText);
    }
    //valid ceasar key cannot be a multiple of 26,
    //will not change the ciphertext

    public boolean setCeasarKey(int key) {
        if ((key % 26) != 0) {
            ceasarKey = key;
            return true;
        }
        return false;
    }

    //sets the key to the key stored to a friend if no key is supplied
    //fails if the message does not have a user and friendID set
    //or friend has no set ceasarkey
    public boolean setCeasarKey() {
        if (super.getUser() != null && super.getFriendID() != 0) {
            Friend tFriend = super.getUser().findFriend(
                    super.getFriendID());
            if (tFriend.getCeasarKey() == 0) {
                return false;
            }
            ceasarKey = tFriend.getCeasarKey();
            return true;
        }
        return false;
    }

    //returns true if the message was encrypted successfully
    //false if message or key are not set/empty
    //encrypts the text of the message
    public boolean encrypt() {
        if (ceasarKey == 0 || super.getText().equals("")) {
            return false;
        }
        String temp = super.getText().toUpperCase();
        String result = "";
        //shift all letters in the text to the right by key
        for (int i = 0; i < temp.length(); i++) {
            char c = temp.charAt(i);
            if (c >= 65 && c <= 90) //check if each character is a letter
            {
                int shift = (int) c;
                shift -= 65;
                shift += ceasarKey;
                shift %= 26;
                shift += 65;
                c = (char) shift;
                //shift char over by key
            }
            result += c;
        }
        super.setText(result);
        return true;
    }

    //returns true if the message was decrypted successfully
    //false if message or key are not set/empty
    //decrypts the text of the message
    public boolean decrypt() {
        if (ceasarKey == 0 || super.getText().equals("")) {
            return false;
        }
        String temp = super.getText().toUpperCase();
        String result = "";
        //shift all letters in the text to the left by key
        for (int i = 0; i < temp.length(); i++) {
            char c = temp.charAt(i);
            if (c >= 65 && c <= 90) //check if each character is a letter
            {
                int shift = (int) c;
                shift -= 65;
                shift -= ceasarKey;
                int modbase = 26;
                while (shift < 0) {
                    shift += modbase;
                }
                shift %= 26;
                shift += 65;
                c = (char) shift;
                //shift char over by key
            }
            result += c;
        }
        super.setText(result);
        return true;
    }

    public static int generateKey() {                                            
        int key = 0;
        Random rand = new Random();
        do {
            key = rand.nextInt(999);
        } while (key % 26 == 0);
        return key;
    }
}